package com.lcm.learn.lcmcode.sort;

import java.util.Arrays;
import java.util.Random;

/**
 * @description:
 * @author: lcm
 * @create: 2020-01-20 14:43
 **/

public class BubbleSort implements Sort{

    public static void main(String[] args){
        BubbleSort bubbleSort=new BubbleSort();
        int len=10000;
        int[] array=new int[len];
        for(int i=0;i<len;i++){
            array[i] = (int)(Math.random()*10000);
        }
        int[] copyArray = Arrays.copyOf(array, array.length);
        Arrays.sort(copyArray);
        //System.out.println("排序前："+Arrays.toString(array));
        long startTime = System.currentTimeMillis();
        bubbleSort.sort(array);
        System.out.println("排序时间/ms："+(System.currentTimeMillis()-startTime));
        //System.out.println("排序后："+Arrays.toString(array));
        System.out.println("是否正确："+Arrays.equals(array,copyArray));
    }

    @Override
    public void sort(int[] array){
        for(int i=0;i<array.length;i++){
            for(int j=0;j<array.length-i-1;j++){
                if(array[j]>array[j+1]){
                    int temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                }
            }
        }
    }

    public static void sortCheck(int[] array){
        for(int i=0;i<array.length;i++){
            boolean isChange=false;
            for(int j=0;j<array.length-i-1;j++){
                if(array[j]>array[j+1]){
                    int temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                    isChange=true;
                }
            }
            if(!isChange) return;
        }
    }
}
